<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>rem的计算</title>
</head>
<body>
	<script>
		 !function(){
            var nowCh,nowCw,pageWidth,ua=window.navigator.userAgent,
                    getWinWidth=function(){
                        var docWidth=document.documentElement.clientWidth;
                        return window.innerWidth?Math.min(docWidth,window.innerWidth):docWidth;
                    },
                    getWinHeight=function(){
                        var docHeight=document.documentElement.clientHeight;
                        return window.innerHeight?Math.min(docHeight,window.innerHeight):docHeight;
                    },
                    isIpad=/ipad/i.test(ua),
                    isSohuVideoClient=/SohuVideoMobile/i.test(ua),
                    ch =getWinHeight(),
                    cw =getWinWidth(),
                    psdWidth = 750,
                    padLandscape=false,
                    ipadLandscape=!!(isIpad&&cw>=1),
                    maxRem = 100;
            if((cw>=1024&&cw>1)||ipadLandscape){
                psdWidth=2048;
                padLandscape=true;
                if(ipadLandscape&&cw<1024){
                    cw=1024;
                }
            }
            var calcRem = function(event) {
                var e=event||window.event,isResize=!!(e&&e.type==='resize');
                if(e){
                    nowCh =getWinHeight();
                    nowCw =getWinWidth();
                    psdWidth=nowCw>=1024&&nowCh>1?2048:750;
                }
                //新增判断在搜狐视频客户端内横屏访问时打开竖屏webview的适配
                if ((window.orientation && (window.orientation == 90 || window.orientation == -90)&&!(isSohuVideoClient&&!e&&cw<500))||(!e&&padLandscape)||(isResize&&nowCwwCh>1)) {
                    if(isResize){
                        pageWidth = Math.max(nowCh,nowCw);
                    }else{
                        pageWidth = Math.max(ch, cw);
                    }
                    if(padLandscape){
                        padLandscape=false;
                    }
                } else {
                    if(isResize){
                        pageWidth = Math.min(nowCh, nowCw);
                    }else{
                        pageWidth = Math.min(ch, cw);
                    }
                }
                var rem = (pageWidth / psdWidth * 100).toFixed(2);
                rem = Math.min(rem, maxRem);
                document.getElementsByTagName('html')[0].style.fontSize = rem + 'px';
            };
            var on=function(el,type,fn,capture){
                if(el.addEventListener){
                    el.addEventListener(type,fn,!!capture);
                }else if(el.attachEvent){
                    el.attachEvent('on'+type,fn);
                }else{
                    el['on'+type]=fn;
                }
            };
            calcRem();
            on(window,'onorientationchange' in window?'orientationchange':'resize',calcRem);
        }();

	</script>
</body>
</html>